package com.fanshuai.springcloud.config;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Configuration
public class FilterConfig {

    @Bean
    public GlobalFilter a() {
        return new FilterA();
    }

    @Bean
    public GlobalFilter b() {
        return new FilterB();
    }

    @Bean
    public GlobalFilter c() {
        return new FilterC();
    }

    public static class FilterA implements GlobalFilter, Ordered {
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            System.out.println("before filterA");
            Mono<Void> result = chain.filter(exchange);
            System.out.println("after filterA");

            return result;
        }

        @Override
        public int getOrder() {
            return 100;
        }
    }

    public static class FilterB implements GlobalFilter, Ordered {
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            System.out.println("before filterB");
            Mono<Void> result = chain.filter(exchange);
            System.out.println("after filterB");

            return result;
        }

        @Override
        public int getOrder() {
            return 200;
        }
    }

    public static class FilterC implements GlobalFilter, Ordered {
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            System.out.println("before filterC");
            Mono<Void> result = chain.filter(exchange);
            System.out.println("after filterC");

            return result;
        }

        @Override
        public int getOrder() {
            return 300;
        }
    }
}
